Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem when running loops with forvalues command: error message: `i' invalid name

    Dear Statalist,

    I got a problem with running a loop.
    I give you my whole code, so you can see how variables/matrices are defined.
    The problem is: forvalues i=......{ } gives me an error message with `i' invalid name.

    I would highly appreciate your help/suggestions.

    Best wishes and thank you very much in advance!

    Nico


    Code:
    set more off 
    preserve 
    drop if aage>0 & aage<=16 
    drop if STATR==2   
    
    reg  lfp   edu pexp pexp2 pexp3 pexp4 i.year i.yuk  i.cohort uratem married fd  if  (age>=25 & age<65) & female==0 & im==1 & yuk<=60, robust   
    
    matrix beta=e(b) // to display matrix values write `matrix list beta' 
    predict yhat if e(sample)
    su yhat if e(sample)
    scalar ybar=r(mean)
    scalar nobs=e(N)
    scalar beta0=beta[1,109] //  _constant is at 109.
    matrix yuk=beta[1,"0.yuk".."60.yuk"]   
    tabulate yuk if e(sample), matcell(yukmeans) // this gives me the absolute frequencies in a matrix or vector
    matrix yukmeans=yukmeans/nobs // now generate the relative frequencies 
    matrix i1=yuk*yukmeans // this is a 1x1 matrix, I checked. 
    scalar i1=trace(i1) //  single value of the matrix and define a scalar 
    scalar i2=ybar-i1  
                    
    
    generate yhat3=0 in 1/61    
    
    forvalues i=0(1)60 {                                             // here is where the error message comes up!!!!!!!!!!!!!! `i' invalid name
        local j = `i’+1
        local alpha = el(yuk,1,`i'.yuk) 
        replace yhat3= beta0 + `alpha'  in `j'     
    }     
             
    
             
    generate x=_n   in 1/45     
        
        
    scatter yhat3  x   
    restore

  • #2
    First, I would note that at least part of the relevant code you have here that precedes the problem could not have executed. I note particularly
    Code:
    matrix yuk=beta[1,"0.yuk".."60.yuk"]
    which is not legal syntax. (It's unusual enough that I don't know what you were trying to do.)

    Regardless of that, there's a problem with
    Code:
    local alpha = el(yuk,1,`i'.yuk)
    The -el()- function expects its last two argument to be numbers, but your `i'.yuk is not a number. One way to show this to yourself is to use -set trace on- at the beginning of your forvalues loop.

    I don't understand what you wanted that statement to do, so I don't have a positive suggestion.. What, for example, did you want 0.yuk, 1.yuk, ..., 60.yuk to contain?


    Comment


    • #3
      You appear to using the acute accent mark rather than the right single quote. Compare:

      Code:
        local j = `i’+1
      Code:
      local j = `i' +1

      Comment


      • #4
        What you show in #1 also is not consistent about quotation marks. Local macro foo is evaluated like this


        Code:
        `foo'
        Although fonts vary, in very many fonts the closing quotation mark appears to be vertical. That is sometimes but not always true of #1. So

        Code:
         
         local j = `i’+1
        should look like

        Code:
         
         local j = `i' + 1

        Comment


        • #5
          Dear Mike, Scott and Nick
          thanks for your kind help and careful reading of my code. I apologize about overlooking such a thing. Now this should take care of the error message. Two questions remain. I have 61 estimates of 61 yuk indicators where the reference category for years since migration or years in UK is year 0 with “estimate” equal to 0. So i.yuk generates 61 values. They all appear in matrix beta = e(b). Basically what I want are two things. First define a new matrix with these 61 regression estimates. This it is what I am trying to do with matrix yuk=beta[1,........] where #2 says not a Stata syntax. When I do the loop with el() I want to extract each of these 61 estimates individually one at a time from the matrix yuk.
          This is it gentlemen!
          Any suggestions I am very grateful for.

          Best wishes

          Nico

          Comment

          Working...
          X